package com.cskaoyan.wx.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.wx.bean.vo.index.FloorGoodsListVo;
import com.cskaoyan.wx.bean.vo.index.GrouponListVo;
import com.cskaoyan.wx.bean.vo.index.WxIndexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class WxIndexServiceImpl implements WxIndexService {

    final static Integer GOODS_COUNT_LIMIT = 4;

    final static Integer FLOOR_COUNT_LIMIT = 4;

    final static Integer BRAND_COUNT_LIMIT = 4;

    final static Integer CATEGORY_COUNT_LIMIT = 10;

    @Autowired
    AdMapper adMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    TopicMapper topicMapper;

    @Autowired
    GrouponRulesMapper grouponRulesMapper;


    @Override
    public WxIndexVo index() {

        WxIndexVo wxIndexVo = new WxIndexVo();
        //banner
        wxIndexVo.setBanner(getBanner());

        //brandList
        wxIndexVo.setBrandList(getBrandList());

        //channel
        wxIndexVo.setChannel(getChannel());

        //floorGoodsList
        wxIndexVo.setFloorGoodsList(getFloorGoodsList());

        //groupListVo
        wxIndexVo.setGrouponList(getGrouponList());

        //couponList
        wxIndexVo.setCouponList(getCouponList());

        //newGoodsList
        wxIndexVo.setNewGoodsList(getNewGoodsList());

        //hotGoodsList
        wxIndexVo.setHotGoodsList(getHotGoodsList());

        //topicList
        wxIndexVo.setTopicList(getTopicList());

        return wxIndexVo;
    }

    private List<Ad> getBanner() {

        AdExample adExample = new AdExample();
        return adMapper.selectByExample(adExample);

    }

    private List<Brand> getBrandList() {

        BrandExample brandExample = new BrandExample();
        brandExample.setOrderByClause("sort_order desc limit " + BRAND_COUNT_LIMIT);
        return brandMapper.selectByExample(brandExample);
    }

    private List<Category> getChannel() {

        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort_order desc limit " + CATEGORY_COUNT_LIMIT);
        categoryExample.createCriteria().andLevelEqualTo("L1");
        return categoryMapper.selectByExample(categoryExample);

    }

    private List<Coupon> getCouponList() {

        CouponExample couponExample = new CouponExample();
        return couponMapper.selectByExample(couponExample);

    }


    private List<FloorGoodsListVo> getFloorGoodsList() {

        ArrayList<FloorGoodsListVo> floorGoodsListVos = new ArrayList<>();

        //先取二级分类
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort_order desc limit " + FLOOR_COUNT_LIMIT);
        categoryExample.createCriteria().andLevelEqualTo("L1");
        List<Category> categories = categoryMapper.selectByExample(categoryExample);

        for (Category category : categories) {

            FloorGoodsListVo floorGoodsListVo = new FloorGoodsListVo();
            floorGoodsListVo.setId(category.getId());
            floorGoodsListVo.setName(category.getName());

            //查询每个分类对应的分类
            Integer id = category.getId();
            CategoryExample categoryExampleL2 = new CategoryExample();
            categoryExampleL2.createCriteria().andPidEqualTo(id);
            categoryExampleL2.setOrderByClause("sort_order desc");
            List<Category> categoriesL2 = categoryMapper.selectByExample(categoryExampleL2);

            List<Goods> goodsList = new ArrayList<>();
            for (Category categoryL2 : categoriesL2) {

                if (goodsList.size() >= FLOOR_COUNT_LIMIT) {
                    break;
                }

                Integer categoryId = categoryL2.getId();

                GoodsExample goodsExample = new GoodsExample();
                goodsExample.createCriteria().andCategoryIdEqualTo(categoryId);
                List<Goods> goods = goodsMapper.selectByExample(goodsExample);

                int index = Math.min(FLOOR_COUNT_LIMIT - goodsList.size(), goods.size());
                for (int i = 0; i < index; i++) {
                    goodsList.add(goods.get(i));
                }

            }
            floorGoodsListVo.setGoodsList(goodsList);
            floorGoodsListVos.add(floorGoodsListVo);
        }
        return floorGoodsListVos;
    }


    private List<GrouponListVo> getGrouponList() {

        ArrayList<GrouponListVo> grouponListVos = new ArrayList<>();

        //先拿到groupon
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);

        for (GrouponRules grouponRule : grouponRules) {

            GrouponListVo grouponListVo = new GrouponListVo();

            Integer goodsId = grouponRule.getGoodsId();
            Goods goods = goodsMapper.selectByPrimaryKey(goodsId);

            grouponListVo.setGoods(goods);
            grouponListVo.setGroupon_member(grouponRule.getDiscount());
            //计算团购的差值
            BigDecimal subtract = goods.getCounterPrice().subtract(grouponRule.getDiscount());
            grouponListVo.setGroupon_price(subtract);

            grouponListVos.add(grouponListVo);
        }
        return grouponListVos;
    }

    private List<Goods> getNewGoodsList() {
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.setOrderByClause("add_time desc limit 4");
        goodsExample.createCriteria().andIsNewEqualTo(true).andDeletedEqualTo(false);
        return goodsMapper.selectByExample(goodsExample);
    }

    private List<Goods> getHotGoodsList() {
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.setOrderByClause("sort_order desc limit 4");
        goodsExample.createCriteria().andIsHotEqualTo(true).andDeletedEqualTo(false);
        return goodsMapper.selectByExample(goodsExample);
    }

    private List<Topic> getTopicList() {

        TopicExample topicExample = new TopicExample();
        topicExample.setOrderByClause("sort_order limit " + GOODS_COUNT_LIMIT);
        return topicMapper.selectByExample(topicExample);

    }

}
